﻿using System;
using System.Linq.Expressions;
using System.Linq;
using MicroDb.Options;
using Microsoft.Extensions.DependencyInjection;

namespace MicroDb.PostgreSql
{
    public class NpgSqlFactory : MicroDbFactory
    {
        private readonly IServiceProvider serviceProvider;

        public NpgSqlFactory(IServiceProvider serviceProvider)
        {
            this.serviceProvider = serviceProvider;
        }

        public override bool Is(string type)
        {
            var list = new[] { "NpgSql", "PostgreSql", "PgSql" };
            return list.Any(a => a.Equals(type, StringComparison.OrdinalIgnoreCase));
        }

        public override SqlExecute CreateExecutor(MicroDbOptions dbSource)
        {
            return ActivatorUtilities.CreateInstance<NpgSqlExecute>(serviceProvider, dbSource);
        }

        public override SqlResolver CreateResolver(Expression expression)
        {
            var resolver = new NpgSqlResolver();
            resolver.LoopResolve(expression);
            return resolver;
        }

        public override TableExecute CreateTableExecute(SqlExecute execute)
        {
            return ActivatorUtilities.CreateInstance<NpgSqlTableExecute>(serviceProvider, execute);
        }
    }
}
